#include "HeadBossWeapon.h"
#include "Resource.h"

HeadBossWeapon::HeadBossWeapon(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, bool isLeft) : Object(x, y)
{	 
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[61]->getFileName()), 125, 0);

	this->pos_x = x;
	this->pos_y = y;
	this->pos_x_old = pos_x;
	this->pos_y_old = pos_y;

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	if (isLeft == true)
	{
		this->isLeft = true;
		this->veloc_x = -HEADBOSS_WEAPON_VX;
		p_sprite->setFrame(0,1);
		p_sprite->setIndex(0);
	} 
	else
	{
		this->isLeft = false;
		this->veloc_x = HEADBOSS_WEAPON_VX;
		p_sprite->setFrame(1,2);
		p_sprite->setIndex(1);
	}

	obj_state = IS_ACTIVING;
}

HeadBossWeapon::~HeadBossWeapon()
{
	if (p_sprite != NULL)
	{
		delete p_sprite;
	}
}

void HeadBossWeapon::render(int cameraX, int cameraY)
{
	if (obj_state != IS_REMOVE)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}
}

OBJECT_TYPE HeadBossWeapon::getObjectType()
{
	return OBJECT_ITEM;
}


void HeadBossWeapon::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	/*switch(obj_state)
	{
	case IS_IDLE:
	break;

	case IS_ACTIVING:
	{
	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	pos_x += veloc_x * gameTime;

	attack(obj_list, gameTime);

	if (abs(pos_x - pos_x_old) > 250)
	{
	changeState(IS_REMOVE);
	}
	}
	break;

	}
	p_sprite->update(gameTime);*/

}

void HeadBossWeapon::update(list<Object*>* obj_list, DxInput* input, float gameTime, Object* simon)
{
	switch(obj_state)
	{
	case IS_IDLE:
		break;

	case IS_ACTIVING:
		{
			width = p_sprite->getTexture()->getWidth();
			height = p_sprite->getTexture()->getHeight();

			pos_x += veloc_x * gameTime;

			attack(obj_list, gameTime, simon);

			if (abs(pos_x - pos_x_old) > 250)
			{
				changeState(IS_REMOVE);
			}
		}
		break;

	}
	p_sprite->update(gameTime);

}

void HeadBossWeapon::changeState(int state)
{
	obj_state = state;
	switch(state)
	{
	case IS_ACTIVING:		
		break;	 

	case IS_REMOVE:
		break;
	}
}

void HeadBossWeapon::attack(list<Object*>* obj_list, float time, Object* simon)
{
	Object *obj;
	list<Object*>::iterator i_obj;

	Box box(this->pos_x,
		this->pos_y - this->height, 
		this->width, 
		this->height,
		veloc_x * time,
		0);

	Box block(simon->getPosX(),
		simon->getPosY() - simon->getHeight(),
		simon->getWidth(),
		simon->getHeight());

	Box boxphase = Collision::GetSweptBroadphaseBox(box);			 

	if (Collision::AABBCheck(boxphase, block))
	{
		if (simon->getObjState() == MS_NORMAL)
		{
			int blood = simon->getBlood();
			blood--;
			simon->setBlood(blood);
			if (simon->getBlood() == 0)
			{
				simon->changeState(MS_DIE);		//simon die
			}
			else
			{
				simon->changeState(MS_LIGHT);	//simon 
			}
		}
	}

	
}